package com.mercycrazy.recruit.rabbit;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.mercycrazy.recruit.constant.RabbitMQConstant.*;

/**
 * rabbitmq 发短信相关死信队列的配置
 *
 * @author <a href="mailto:1443424326@qq.com">mercycrazy</a>
 * @since 2023-01-19
 */
@Configuration
public class RabbitMQSMSDeadConfig {

    /**
     * 创建交换机
     */
    @Bean(SMS_EXCHANGE_DEAD)
    public Exchange exchange() {
        return ExchangeBuilder
                .topicExchange(SMS_EXCHANGE_DEAD)
                .durable(true)
                .build();
    }

    /**
     * 创建队列
     */
    @Bean(SMS_QUEUE_DEAD)
    public Queue queue() {
        return QueueBuilder
                .durable(SMS_QUEUE_DEAD)
                .build();
    }

    /**
     * 定义队列绑定到交换机的关系
     */
    @Bean
    public Binding smsDeadBinding(
            @Qualifier(SMS_EXCHANGE_DEAD) Exchange exchange,
            @Qualifier(SMS_QUEUE_DEAD) Queue queue
    ) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with(SMS_ROUTING_DEAD)
                .noargs();
    }
}
